package service

import (
	"fiberdemo/app/db"
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/session"
	"log"
)

var User = newUserService()

type userService struct {
}

func newUserService() *userService {
	return &userService{}
}

func (s *userService) Login(c *fiber.Ctx, username, passwd string) bool {

	//调用db login
	user, err := new(db.User).Login(username, passwd)

	if err != nil {
		log.Printf("login db error : %s", err)
		return false
	}

	if user == nil {
		return false
	}

	//写入redis或session&cookie
	//c.Cookie(&fiber.Cookie{
	//	Name:     "token",
	//	Value:    "randomvalue",
	//	Expires:  time.Now().Add(24 * time.Hour),
	//	HTTPOnly: true,
	//	SameSite: "lax",
	//})

	store := session.New()
	sess, err := store.Get(c)
	defer sess.Save()
	sess.Set("name", "frans")

	return true
}
